﻿
// 选择图片
function uploadFile(obj) {
    
    if ($("#fileurlGuid").val() == "") {
        
        //增加标识
        $("#fileurlGuid").val(JSGuidXX());
    }

    var file_upload = obj.files[0];

    var strext = file_upload.name.substring(file_upload.name.lastIndexOf('.')).toLowerCase();
    var picName = JSGuidXX() + strext;

    // debugger
    $("#file_uploadInfo_progress").show();
    document.getElementById("file_uploadInfo_progress").src = "/common/images/uploading.gif";

    var reader = new FileReader();
    reader.readAsDataURL(file_upload);
    //原文件名
    reader.onload = function (e) { // reader onload start
        // ajax 上传图片

        $.post("/Common/addFileToServerforBase64", { "flag": Math.random(), "oldfilename": picName, base64stringdata: e.target.result }, function (data) {

            var Result = eval(data);
            console.log(Result);
            if (Result.respCode != "200") {
                document.getElementById("file_uploadInfo_progress").src = "/common/images/err.jpg";
                return;
            }
            else {
                saveInfoFileTemp($("#fileurlGuid").val(), file_upload.name, Result.data);
                $("#file_uploadInfo_progress").show();
                document.getElementById("file_uploadInfo_progress").src = "/common/images/ok.png";
            }

        }, 'json');
    } 
}
function JSGuidXX() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

//保存文件
function saveInfoFileTemp(guid, filename, fileurl) {
    var data = null; //自动将form表单封装成json
    $.post("saveInfoFileTemp?guid=" + guid + "&fileurl=" + fileurl + "&name=" + filename, data, function (result) {
        var jsonresult = (result);
        
        if (jsonresult.respCode == "200") {
           // window.location.href = "list";

            showInfoFileTemp(guid)
        }
        else {
           // alert(jsonresult.respMsg);
            return;
        }
    });
}

//显示文件
function showInfoFileTemp(guid) {
    var data = null; //自动将form表单封装成json
    $.post("getInfoFileTemp?guid=" + guid , data, function (result) {
        var jsonresult = (result);
        document.getElementById("Div_showImgInfo").innerHTML = "";
        if (jsonresult.respCode == "200") {
            var filesarr = jsonresult.data;
            for (var i = 0; i < filesarr.length; i++) {
                document.getElementById("Div_showImgInfo").innerHTML += "<div style=''><a href='" + filesarr[i].url + "' target='_blank'>" + filesarr[i].name + "</a>&nbsp;&nbsp;<a href='#'  onclick=\"delInfoFileTemp('" + guid +"','" + filesarr[i].url + "')\">删除</a></div>";
            }

        }
        else {
            // alert(jsonresult.respMsg);
            return;
        }
    });
}

//删除文件
function delInfoFileTemp(guid,fileurl) {
    var data = null; //自动将form表单封装成json
    $.post("delInfoFileTemp?guid=" + guid+"&fileurl=" + fileurl, data, function (result) {
        var jsonresult = (result);
       
        if (jsonresult.respCode == "200") {
            showInfoFileTemp(guid);
        }
        else {
            // alert(jsonresult.respMsg);
            return;
        }
    });
}